package org.strongme.modules.project.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.strongme.common.persistence.Page;
import org.strongme.common.util.excel.ExportExcel;
import org.strongme.common.web.BaseController;
import org.strongme.modules.project.entity.PpRecord;
import org.strongme.modules.project.entity.Project;
import org.strongme.modules.project.entity.SubProject;
import org.strongme.modules.project.service.PpRecordService;
import org.strongme.modules.project.service.ProjectService;
import org.strongme.modules.project.service.SubProjectService;
import org.strongme.util.DateUtils;
import org.strongme.util.StringUtils;

import com.google.common.collect.Lists;

@Controller
@RequestMapping("project/ppRecord")
public class PpRecordController extends BaseController{
	
	@Autowired
	private PpRecordService ppRecordService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private SubProjectService subProjectService;
	
	@ModelAttribute("ppRecord")
	public PpRecord get(@RequestParam(required=false) String id) {
		PpRecord ppRecord = null;
		if (StringUtils.isNotBlank(id)){
			ppRecord = ppRecordService.get(new PpRecord(id));
		}else{
			ppRecord = new PpRecord();
		}
		return ppRecord;
	}
	
	@RequestMapping(value = {"list",""})
	public String list(PpRecord ppRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PpRecord> page = ppRecordService.findPage(new Page<PpRecord>(request, response), ppRecord); 
        model.addAttribute("page", page);
        model.addAttribute("ppRecord", ppRecord);
		return "modules/project/ppRecord/ppRecordList.jsp";
	}
	
	@RequestMapping(value = "form")
	public String form(PpRecord ppRecord, Model model) {
		model.addAttribute("ppRecord", ppRecord);
		model.addAttribute("projectList", projectService.findList(new Project()));
		return "modules/project/ppRecord/ppRecordForm.jsp";
	}
	
	@RequestMapping(value = "save" ,method=RequestMethod.POST)
	public String save(PpRecord ppRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, ppRecord)){
			return form(ppRecord, model);
		}
		ppRecordService.save(ppRecord);
		addMessage(redirectAttributes, "保存拍片记录数据成功");
		return "redirect:/project/ppRecord";
	}
	
	@RequestMapping(value = "delete" ,method=RequestMethod.GET)
	public String delete(PpRecord ppRecord, Model model, RedirectAttributes redirectAttributes) {
		ppRecordService.delete(ppRecord);
		addMessage(redirectAttributes, "删除拍片记录数据成功");
		return "redirect:/project/ppRecord";
	}
	
	
	@RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(PpRecord ppRecord, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = new String(("现场拍片记录表").getBytes("gbk"),"iso8859-1")+DateUtils.getDate()+".xlsx";
            List<PpRecord> list = ppRecordService.findList(ppRecord);
            if(list==null)list=  Lists.newArrayList();
            List<PpRecord> newResult = Lists.newArrayList();
            for(int i=0;i<list.size();i++) {
            	PpRecord ptmp = list.get(i);
            	ptmp.setXh(i+1);
            	newResult.add(ptmp);
            }
            List<String> subTitles = Lists.newArrayList();
            String sub = "";
            if(newResult.size()>0) {
            	PpRecord pp = newResult.get(0);
            	Project p = projectService.get(pp.getProject());
            	SubProject sp = subProjectService.get(pp.getSubProject());
            	if(p!=null &&p.getName()!=null&&sp!=null&&sp.getName()!=null)
            	sub += "工程名称："+p.getName()+"\u0020\u0020\u0020\u0020\u0020\u0020单位工程："+sp.getName();
            }
            String zz = ppRecord.getZz()==null?"":ppRecord.getZz();
            String gh = ppRecord.getGh()==null?"":ppRecord.getGh();
            String jcks = ppRecord.getKsrq()==null?"":ppRecord.getKsrq();
            String jcjs = ppRecord.getJsrq()==null?"":ppRecord.getJsrq();
//            sub+="\u0020\u0020\u0020\u0020\u0020\u0020装置："+zz;
//            sub+="\u0020\u0020\u0020\u0020\u0020\u0020工号："+gh;
            sub+="\u0020\u0020\u0020\u0020\u0020\u0020检测日期："+jcks+"\u0020到\u0020"+jcjs;
            subTitles.add(sub);
    		new ExportExcel("现场拍片记录表",subTitles, PpRecord.class).setDataList(newResult).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出现场拍片记录表失败！失败信息："+e.getMessage());
		}
		return "redirect:/project/ppRecord";
    }
}
